package com.yuncheng.oaApi.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.entity.OaProcessBusiness;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.mapper.OaSpcySqsxJobMapper;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.oaApi.vo.OaJobVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component(value = "OaMyInfoService")
public class OaMyInfoService {

    @Resource
    @Lazy
    private IOaProcessBusinessService oaProcessBusinessService;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private OaSpcySqsxJobMapper oaSpcySqsxJobMapper;

    /**
     * 获取当前科室的流程任务数量
     * @return
     */
    public Map<String, Long> getNowUserKsHjmcRwlInStat(){


        ArrayList list = new ArrayList();
        //拿到所有流程的所有数据
        Map<String, Long> resultMap = new HashMap<>();
        String orgName = GetCurrentUserUtils.getCurrentUser().getOrgName();
        String flcbm = "OA_LGQS,OA_QXJ,OA_JBZB,OA_XZSWSP,OA_XCXXSP,OA_HYSSQGL,OA_BGYPSQ,NK_CCSP";
        List<OaProcessBusiness> oaProcessBusinesses = new ArrayList();

        List<String> lcbmList = Arrays.asList(flcbm.split("[,，]"));
        for (String lcbm : lcbmList){
            Long count = oaProcessBusinessService.count(new LambdaQueryWrapper<OaProcessBusiness>()
                    .eq(OaProcessBusiness::getFsqbm, orgName)
                    .eq(OaProcessBusiness::getFywlx, OaFlowConstant.OA)
                    .eq(OaProcessBusiness::getFlcmb, lcbm));
            resultMap.put(lcbm, count);
        }

        return resultMap;
    }



    /**
     * 获取本月本科室-人员任务量
     */
    public List<Map> getNowUserKsryRwlInStat(String orgId){
        List<Map> resultList = new ArrayList<>();

        List<YcSysUser> userList = ycSysUserService.getDeptIndexUserList(orgId);

        String month = DateConversionUtils.DateToMonth(new Date());
        List<OaJobVo> jobList = oaSpcySqsxJobMapper.selectMySpcyRwlList(null, OaFlowConstant.OA, null,null, month);

        for (int i = 0; i < userList.size(); i++) {
            String userid = userList.get(i).getId();
            long dbsl = jobList.stream()
                    .filter(f -> !f.getFblzt().equals(OaCommonFlowConstant.BLZT_YB) && f.getFdbrid().contains(userid))
                    .collect(
                            Collectors.collectingAndThen(
                                    Collectors.toCollection(
                                            () -> new TreeSet<>(
                                                    Comparator.comparing(o -> o.getFpid())
                                            )
                                    ), ArrayList<OaJobVo>::new
                            )
                    )
                    .size();

            long ybsl = jobList.stream()
                    .filter(f -> f.getFblzt().equals(OaCommonFlowConstant.BLZT_YB) && f.getFdbrid().contains(userid))
                    .collect(
                            Collectors.collectingAndThen(
                                    Collectors.toCollection(
                                            () -> new TreeSet<>(
                                                    Comparator.comparing(o -> o.getFpid())
                                            )
                                    ), ArrayList<OaJobVo>::new
                            )
                    )
                    .size();

            long zsl = jobList.stream()
                    .filter(job -> job.getFdbrid().contains(userid))
                    .collect(
                            Collectors.collectingAndThen(
                                    Collectors.toCollection(
                                            () -> new TreeSet<>(
                                                    Comparator.comparing(o -> o.getFpid())
                                            )
                                    ), ArrayList<OaJobVo>::new
                            )
                    )
                    .size();

            Map dataMap = new HashMap();
            dataMap.put("index", userList.get(i).getRealname());

            Map resultMap = new HashMap();
            resultMap.put("dbsl", dbsl);
            resultMap.put("ybsl", ybsl);
            resultMap.put("zsl", zsl);

            dataMap.put("data", resultMap);
            resultList.add(dataMap);
        }

        return resultList;
    }






}
